package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.ContractComments;
import com.logic.landseaserver.service.IContractCommentsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Path("/contractComments")
@Api(value="contractComments")
public class ContractCommentsRest {
	@Autowired
	private IContractCommentsService contractCommentsService;

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	//备注列表
	@POST
	@Path("/list")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "备注列表")
	public Response contractCommentsList(
			@ApiParam(value = "param", required = false) @QueryParam("contractId") Integer contractId, QueryUtil queryUtil) {
		CommonResultMap map = new CommonResultMap();
		List<Object> list = null;
		try {
			queryUtil.getPagingTool().calculateStartIndex();
			list = contractCommentsService.queryByPage(queryUtil,contractId);
			Integer totalCount = queryUtil.getPagingTool().getTotalNum();
			map.setData(list);
			map.setResultTotalCount(totalCount);
			map.setResult(CommonResult.SUCCESS);
		} catch (Exception e) {
		    logger.error("contractCommentsList error", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	@POST
	@Path("/addComment")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "添加备注")
	public Response createComments(
			@ApiParam(value = "param", required = true) ContractComments contractComments) {  
		CommonResultMap map = new CommonResultMap();
		try {
			Integer id = contractCommentsService.insert(contractComments);
			map.setData(id);
			map.setResult(CommonResult.SUCCESS, "Create success");
		} catch (BusinessException e) {
		    logger.error("createComments error", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		} catch (Exception e) {
		    logger.error("createComments error", e);
			map.setResult(CommonResult.ERROR, e.getMessage());
		}
		return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}

}
